package sawyer;
/** Main program for the TwitterReader program. 
 * @author shadowimmage@gmail.com - Chase Sawyer
 *-Update 5/17/13: no longer need SerialIO since not using Arduino for physical display. Will instead use
 *Java's 2D graphics engine.
 */


import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

public class Main {
	private static boolean running = false;
	private static TwitInterface twit;
	
	private static String term = "";
	private static String paragraph = "";
	private static final int maxWords = 260;
	private static int count = 0;
	private static List<String> seenWords = new LinkedList<String>();

	public static void main(String[] args) {
		Scanner consoleIn = new Scanner(System.in);
		Debug debug = new Debug(true);
		
		Display screen = new Display(debug, 1024, 768);
		
		twit = new TwitInterface(debug);
		while (!TwitInterface.initiated()) {
			// wait until system has communicated with server and is ready.
		}
		System.out.println(twit.getScreenName());
		System.out.println("Enter starting word: ");
		String s = consoleIn.nextLine();
		System.out.println("Running... Enter 'halt' to quit.");
		term = s;
		running = true;
		
		while (!Display.isInitiated()) {
			// wait for graphics to be started...
		}
		search(); // begin first search.
		while (running) {
			s = consoleIn.nextLine();
			s = s.toLowerCase();
			s = s.trim();
			if (s.matches("halt")) {
				running = false;
			} 
		}
		// ENDING PROGRAM:
		twit.shutdown();
		consoleIn.close();
		screen.close();
	}
	
	/** Called once a result has been found by the twitInterface. 
	 * @param word
	 */
	public static void result(String word) {
		word = word.trim();
		if (!seenWords.contains(word)) {
			seenWords.add(word);
		}
		count++;
		if (count > maxWords) {
			if (!word.matches("\\w+\\.")) {
				word += ".";
			}
			appendWord(word);
			count = 0;
			FileIO sentenceOutput = new FileIO("sentence.txt");
			FileIO wordsOutput = new FileIO("words.txt");
			sentenceOutput.writeFile(paragraph);
			wordsOutput.writeFile(seenWords.toString());
			try {
				Thread.sleep(10000); // sleep for 10 seconds.
			} catch(InterruptedException ex) {
				Thread.currentThread().interrupt();
			}
			clearParagraph();
			chooseNewSearch();
		} else {
			appendWord(word);
			term = word;
			search();
		}
	}
	
	/** Add the given word to the end of the printed text string on-screen.
	 * @param word - The string to be appended.
	 */
	public static void appendWord(String word) {
		word += " ";
		paragraph += word;
	}
	
	/** Called to begin the search, given the local term. Results will come back to result().
	 */
	private static void search() {
		twit.search(term, 100);
	}
	
	public static String getCurrentTerm() {
		return term;
	}
	
	public static String getParagraph() {
		return paragraph;
	}
	
	private static void chooseNewSearch() {
		int item = new Random().nextInt(seenWords.size());
		term = seenWords.get(item);
		search();
	}
	
	private static void clearParagraph() {
		paragraph = "";
	}
}
